Two new routines for retrieving the pixel dimensions
of text that will be drawn on the screen have been added
to QuickDraw Text. These routines can be called from
PowerPC applications linking against the shared library
FontManager
in the System file. The new
routines are defined as follows:
QDTextBounds
void QDTextBounds(short byteCount,
const void* textAddr,
Rect* bounds);
byteCount
is the number of bytes of text
contained in the buffer located at the address contained
in the value textAddr
.
textAddr
points to byteCount
bytes of textual data.
bounds
is a pointer to a
Rect
structure whose coordinates will be
calculated by QDTextBounds
. On return,
bounds will contain the bounding coordinates for the
entire image (including parts of the image that may
extend beyond the first and last pen positions after the
text has been drawn) that will be drawn for the text
(given the font settings in the current
GrafPort
). Note that the coordinates
returned are relative to the current pen position (as if
the pen were located at the origin). For example, in
Listing 3, we use QDTextBounds
to draw a
rectangle around the text's image. Notice how in this
example the bounds returned by QDTextBounds
are offset using the current pen position, so that the
text drawing and the rectangle drawing occur using the
same coordinate system.
Listing 3. Using QDTextBounds
to
discover where text will be drawn.
Rect bounds;
char *text = "sample text";
Point where;
SetPt(&where, 100, 100);
MoveTo(where.h, where.v);
PenSize(1,1);
QDTextBounds(text, strlen(text), &bounds);
OffsetRect(&bounds, where.h, where.v);
InsetRect(&bounds, -1, -1);
FrameRect(&bounds);
DrawText(text, 0, strlen(text));
The leftmost edge of the text's image can either be
to the right or the left of the pen postition, and the
rightmost edge of the text's image may be to the left or
the right of the final pen position.
FetchFontInfo
OSErr FetchFontInfo(SInt16 fontID,
SInt16 fontSize,
SInt16 fontStyle,
FontInfo* info);
fontID
is the font ID number for a
font.
fontSize
is the font size in pixels.
fontStyle
contains the font style flags
for the font.
info
is a pointer to a font information
record where the result will be stored.
FetchFontInfo
returns the same
information as GetFontInfo,
except, rather
than gathering information about the font settings from
the current GrafPort,
this information is
provided as parameters to the routine. If
FetchFontInfo
returns an error, the fields
in the FontInfo
record will be set to zero.
(The error code returned is the value returned by
FMSwapFont
.)
Related Materials: